package com.vvv.designpattern.ordermanagement.audit;

import java.util.Date;
import java.util.Map;

/**
 * @author vvv
 * @date 2023-11-26 15 02
 * discription 模板模式：抽象类中定义好做一件事情的步骤，其中一些步骤是已经实现好了的（基础方法）
 * ，另外一些步骤是交给子类去实现的（模板方法）。
 *
 */
public abstract class AbstractAuditLogProcessor {

    // 基础方法
    private final OrderAuditLog basicAuditLog(int userId, String action, String orderId) {
        OrderAuditLog orderAuditLog = new OrderAuditLog();
        orderAuditLog.setUserId(userId);
        orderAuditLog.setAction(action);
        orderAuditLog.setOrderId(orderId);
        orderAuditLog.setDate(new Date());
        return orderAuditLog;
    }
    // 模板方法
    protected abstract OrderAuditLog buildDetails(OrderAuditLog orderAuditLog, Map<String, Object> details);
    // 定义好的步骤
    public final OrderAuditLog createAuditLog(int userId, String action, String orderId) {
        return this.createAuditLog(userId, action, orderId, null);
    }

    public final OrderAuditLog createAuditLog(int userId, String action, String orderId, Map<String, Object> details) {
        OrderAuditLog orderAuditLog = basicAuditLog(userId, action, orderId);
        return buildDetails(orderAuditLog, details);
    }
}
